// pages/mine/collection/collection.js
const api = require('../../../api/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: wx.getStorageSync("isLogin"),
    collectionGoods: [],
    page: 1,
    pageSize: 12,
    hasMoreData: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      isLogin: wx.getStorageSync("isLogin"),
    });
    that.getCollectionGoods();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  getCollectionGoods: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    });
    wx.request({
      url: api.LikedProduct,
      data: {
        page: that.data.page,
        per_page: 12,
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json',
        'X-bocang-Authorization': wx.getStorageSync('token')
      },
      success: function(res) {
        var goodsListTmp = that.data.collectionGoods;
        if (res.data.error_code == 0) {
          if (that.data.page == 1) {
            goodsListTmp = [];
          }
          if (res.data.products.length < that.data.pageSize) {
            that.setData({
              collectionGoods: goodsListTmp.concat(res.data.products),
              hasMoreData: false,
            });
          } else {
            that.setData({
              collectionGoods: goodsListTmp.concat(res.data.products),
              hasMoreData: true,
              page: that.data.page + 1,
            });
          }
        }
      }
    });
    setTimeout(function() {
      wx.hideLoading();
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let that = this;
    if (that.data.hasMoreData) {
      that.getCollectionGoods();
    } else {
      wx.showToast({
        title: '没有更多数据',
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})